package com.aigou.admin.controller;


import com.aigou.admin.entity.PageEntity;
import com.aigou.admin.entity.ResponseEntity;
import com.aigou.admin.entity.Shop_Cart_Product;
import com.aigou.admin.service.Shop_Cart_ProductService;
import com.aigou.admin.service.impl.Shop_Cart_ProductServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/shopCartProduct")
public class Shop_Cart_ProductController extends BaseServlet{
    private Shop_Cart_ProductService service=new Shop_Cart_ProductServiceImpl();

    public void save(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        String id = req.getParameter("id");
        String shop_cart_id = req.getParameter("shop_cart_id");
        String product_id = req.getParameter("product_id");
        String product_num1 = req.getParameter("product_num");
        int product_num = Integer.parseInt(product_num1);
        Shop_Cart_Product shopCartProduct=new Shop_Cart_Product(id,shop_cart_id,product_id,product_num);
        Boolean rs = service.save(shopCartProduct);
        ResponseEntity.ok(resp,rs);
    }

    public void update(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        String id = req.getParameter("id");
        String shop_cart_id = req.getParameter("shop_cart_id");
        String product_id = req.getParameter("product_id");
        String product_num1 = req.getParameter("product_num");
        int product_num = Integer.parseInt(product_num1);
        Shop_Cart_Product shopCartProduct=new Shop_Cart_Product(id,shop_cart_id,product_id,product_num);
        Boolean rs = service.update(shopCartProduct);
        ResponseEntity.ok(resp,rs);
    }

    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<Shop_Cart_Product> list = service.selectAll();
        ResponseEntity.ok(resp,list);
    }


    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        Boolean rs = service.remove(id);
        //查询用户
        ResponseEntity.ok(resp,rs);
    }

    //分页查询
    public void page(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String current = req.getParameter("current");
        String pageSize = req.getParameter("pageSize");
        //查询用户
        PageEntity<Shop_Cart_Product> cart_product = service.findPage(Integer.parseInt(current), Integer.parseInt(pageSize));
        ResponseEntity.ok(resp,cart_product);
    }
}
